<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {field_type, iframe, in_right_admin} from "@/js/tool.js";
import {decode_config, param_encode} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name', '');

    if(in_right_admin('ModelV2WhereParam/add')) btns['添加'] = function(){ iframe('model_v2_where_param/edit?'+app.param()) }
    if(in_right_admin('ModelV2WhereParam/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let method_map = {
      'eq':'等于', 'neq':'不等于',
      'gt':'大于', 'egt':'大于或等于',
      'lt':'小于', 'elt':'小于或等于',
      'like':'包含', 'notlike':'不包含',
      'in':'在列表中', 'notin':'不在列表中',
      'between':'介于之间', 'notbetween':'不介于之间',
    };
    let type_map = field_type(field_type.CAN_MODEL_FIELD, 'label');

    let forms_config = {
      model_name: {label:'模型',name:'where[model_name][eq]',type:'select_one',config:[],value:''},
      label: {label:'关键词',name:'where[label|tips][like]',type:'text_one',config:[],value:''},
    };
    if (model_name) {
      forms_config.model_name.type = "hidden";
      forms_config.model_name.value = model_name;
    }
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: '', order:true},
      model_name:{name:'model_name', label:'模型', width:'100px',type:'select_one',config:[]},
      field: {label:'字段名',name:'field',type:'text_one',value:''},
      method:{name:'method', label:'检索方式', type:'select_one', config:method_map, width:'80px'},
      label:{name:'label', label:'别名', width:'100px'},
      tips:{name:'label', label:'介绍'},
      type:{name:'type', label:'字段类型',type:'select_one',config:type_map,width:'100px'},
      value:{name:'value', label:'默认值'},

      sort:{name:'sort', label:'排序', type:'text_one', width:'60px', can_edit:true, onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'60px', type:'action', fixed:'right'};

    return {
      model_name: model_name,
      config: {
        api: '/admin/model_v2_where_param/'+(is_del?'recycle':'list'),
        title: "模型字段列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      return param_encode(data);
    },
    to_edit(row){
      let can_edit = in_right_admin('ModelV2WhereParam/edit');
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('model_v2_where_param/edit?'+this.param(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('ModelV2WhereParam/edit')) return;
      ajax(`/admin/model_v2_where_param/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (res.data==0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/index/model_v2_list',cache:true},(res)=>{
      this.config.list_cols.model_name.config = decode_config(res.data.list, {content_key:'label', value_key:'name'});
      this.config.forms_config.model_name.config = decode_config(res.data.list, {content_key:'label', value_key:'name'});
      return false
    });


  }
}
</script>
